//
//  XGBaseSettingTableViewController.m
//  Lottery
//
//  Created by monkey on 2018/10/7.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <MJExtension/MJExtension.h>

#import "XGBaseSettingTableViewController.h"

#import "XGSettingTableViewCell.h"

#import "XGSettingGroupModel.h"
#import "XGSettingItemModel.h"

@interface XGBaseSettingTableViewController ()

/** 组模型数组 */
@property (nonatomic,strong) NSArray<XGSettingGroupModel *> *groupsArray;

@end

@implementation XGBaseSettingTableViewController
{
    /// 要解析的plist文件名
    NSString *_plistName;
}

#pragma mark - 构造方法

- (instancetype)init
{
    NSException *exception = [NSException exceptionWithName:@"创建对象错误,创建对象时必须绑定一个plist文件" reason:@"请使用 +tableViewControllerWithPlistName: 或 -initWithPlistName:方法创建对象" userInfo:nil];
    [exception raise];
    return nil;
}

- (instancetype)initWithPlistName:(NSString *)plistName
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        _plistName = plistName;
    }
    
    return self;
}

+ (instancetype)tableViewControllerWithPlistName:(NSString *)plistName
{
    return [[self alloc] initWithPlistName:plistName];
}

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.rowHeight = 64;
}

#pragma mark - UITableViewDataSource && UITableViewDelegate

// 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupsArray.count;
}

// 每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupsArray[section].items.count;
}

// 每行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XGSettingItemModel *itemModel = self.groupsArray[indexPath.section].items[indexPath.row];
    XGSettingTableViewCell *cell = [XGSettingTableViewCell cellForTableView:tableView itemModel:itemModel];
    return cell;
}

// 每组的组头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.groupsArray[section].header;
}

// 每组的组尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return self.groupsArray[section].footer;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 获取模型
    XGSettingItemModel *itemModel =  self.groupsArray[indexPath.section].items[indexPath.row];
    // 根据模型创建控制器
    UIViewController *viewController = nil;
    
    if (itemModel.pushViewController == nil && itemModel.funcName == nil) {
        return;
    } else if (itemModel.funcName != nil) {
        SEL func = NSSelectorFromString(itemModel.funcName);
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:func];
        #pragma clang diagnostic pop
        return;
    } else if (itemModel.plistName != nil) {
        viewController = [[NSClassFromString(itemModel.pushViewController) alloc] initWithPlistName:itemModel.plistName];
    } else {
        viewController = [[NSClassFromString(itemModel.pushViewController) alloc] init];
    }
    
    viewController.title = itemModel.title;
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - 懒加载

- (NSArray<XGSettingGroupModel *> *)groupsArray
{
    if (!_groupsArray) {
        _groupsArray = [[XGSettingGroupModel mj_objectArrayWithFilename:[_plistName stringByAppendingPathExtension:@"plist"]] copy];
    }
    
    return _groupsArray;
}

@end
